package com.fh.shop.api.util;

import redis.clients.jedis.Jedis;

import java.util.Map;

public class RedisUtil {


    public static void set(String key, String value) {
        Jedis connection = null;
        try {
            //从连接池获取路径
            connection  = RedisPool.getConnection();
            connection .set(key,value);
        } catch (Exception e) {
            e.printStackTrace();
           //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
    }


    public static boolean exist(String key) {
        Jedis connection = null;
        boolean exist = false;
        try {
            //从连接池获取路径
            connection  = RedisPool.getConnection();
            exist = connection.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
        return exist;
    }


    public static void expire(String key,int seconds) {
        Jedis connection = null;
        boolean exist = false;
        try {
            //从连接池获取路径
            connection  = RedisPool.getConnection();
            connection.expire(key,seconds);
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
    }


    public static String get(String key){
        Jedis connection = null;
        String result = "";
        try {
            //从连接池获取路径
            connection  = RedisPool.getConnection();
            result = connection.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            //缓存服务器，宕机了，还可以去数据库获取数据，
            //如果在此处抛异常，就不会去数据库获取数据，
           //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
        // 有了throw，就可以不要return，否则就得加上 return
        //throw new RuntimeException("===");

         return result;
    }


    public static long delete(String key){
        Jedis connection = null;
        long res = -1;
        try {
            //从连接池获取路径
            connection  = RedisPool.getConnection();
            res = connection.del(key);
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
        return res;
    }


    //自动刷新
    public static void setEx(String key,String value ,int seconds){
        Jedis connection = null;
        try {
            //从连接池获取路径
            connection = RedisPool.getConnection();
            connection.setex(key,seconds,value);
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
    }

    public static void hset(String key,String field, String value) {
        Jedis connection = null;
        try {
            //从连接池获取路径
            connection  = RedisPool.getConnection();
            connection.hset(key,field,value);
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
    }


    public static void hmset(String key, Map<String,String> map) {
        Jedis connection = null;
        try {
            //从连接池获取路径
            connection  = RedisPool.getConnection();
            connection.hmset(key,map);
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
    }

    public static String hget(String key, String field) {
        Jedis connection = null;
        String res = "";
        try {
            //从连接池获取路径
            connection  = RedisPool.getConnection();
            res = connection.hget(key,field);
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        } finally {
            if (connection != null){
                connection.close();
            }
        }
        return res;
    }

}
